package com.edu.web.controller.info;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.edu.common.core.domain.entity.SysDictData;
import com.edu.common.utils.DictUtils;
import com.edu.system.domain.TClassExt;
import com.edu.system.domain.TTeacherExt;
import com.edu.system.domain.dto.LessonTimetableDTO;
import com.edu.system.domain.dto.WeekTimetableDTO;
import com.edu.system.domain.req.WeekTimetableReq;
import com.edu.system.service.ITClassExtService;
import com.edu.system.service.ITTeacherExtService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.edu.common.annotation.Log;
import com.edu.common.core.controller.BaseController;
import com.edu.common.core.domain.AjaxResult;
import com.edu.common.enums.BusinessType;
import com.edu.system.domain.TLessonTimetable;
import com.edu.system.service.ITLessonTimetableService;
import com.edu.common.utils.poi.ExcelUtil;
import com.edu.common.core.page.TableDataInfo;

/**
 * 课Controller
 * 
 * @author xxxx
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/system/timetable")
public class TLessonTimetableController extends BaseController
{
    @Autowired
    private ITLessonTimetableService tLessonTimetableService;
    @Resource
    private ITTeacherExtService itTeacherExtService;
    @Resource
    private ITClassExtService itClassExtService;

    /**
     * 查询课列表
     */
    @PreAuthorize("@ss.hasPermi('system:timetable:list')")
    @GetMapping("/list")
    public TableDataInfo list(TLessonTimetable tLessonTimetable)
    {
        startPage();
        List<TLessonTimetable> list = tLessonTimetableService.selectTLessonTimetableList(tLessonTimetable);
        return getDataTable(list);
    }

    /**
     * 导出课列表
     */
    @PreAuthorize("@ss.hasPermi('system:timetable:export')")
    @Log(title = "课", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TLessonTimetable tLessonTimetable)
    {
        List<TLessonTimetable> list = tLessonTimetableService.selectTLessonTimetableList(tLessonTimetable);
        ExcelUtil<TLessonTimetable> util = new ExcelUtil<>(TLessonTimetable.class);
        util.exportExcel(response, list, "课数据");
    }

    /**
     * 获取课详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:timetable:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tLessonTimetableService.selectTLessonTimetableById(id));
    }

    /**
     * 新增课
     */
    @PreAuthorize("@ss.hasPermi('system:timetable:add')")
    @Log(title = "课", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TLessonTimetable tLessonTimetable)
    {
        return toAjax(tLessonTimetableService.insertTLessonTimetable(tLessonTimetable));
    }

    /**
     * 修改课
     */
    @PreAuthorize("@ss.hasPermi('system:timetable:edit')")
    @Log(title = "课", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TLessonTimetable tLessonTimetable)
    {
        return toAjax(tLessonTimetableService.updateTLessonTimetable(tLessonTimetable));
    }

    /**
     * 删除课
     */
    @PreAuthorize("@ss.hasPermi('system:timetable:remove')")
    @Log(title = "课", businessType = BusinessType.DELETE)
	  @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tLessonTimetableService.deleteTLessonTimetableByIds(ids));
    }

    /**
     * 获取课程颜色
     */
	  @GetMapping("/getLessonColor")
    public AjaxResult getLessonColor()
    {
        List<SysDictData> lesson = DictUtils.getDictCache("lesson");
        if (lesson == null){
            return AjaxResult.success();
        }
        Map<String, String> collect = lesson.stream()
            .collect(Collectors.toMap(SysDictData::getDictLabel, SysDictData::getDictValue, (x, y) -> x));
        System.out.println(ObjectUtil.toString(collect));
        return AjaxResult.success(collect);
    }

    /**
     * 班级课表
     * @param req
     * @return
     */
    @PostMapping("/classTimetable")
    public AjaxResult classTimetable(@RequestBody WeekTimetableReq req){
        WeekTimetableDTO dto = tLessonTimetableService.buildWeekTimetable(req, tLessonTimetableService.CLASS_TABLE);
        return AjaxResult.success(dto);
    }

    /**
     * 老师课表
     * @param req
     * @return
     */
    @PostMapping("/teacherTimetable")
    public AjaxResult teacherTimetable(@RequestBody WeekTimetableReq req){
        logger.info(JSONObject.toJSONString(req));
        if (StrUtil.isBlank(req.getEid())){
            List<TTeacherExt> t = itTeacherExtService.selectTTeacherExtList(new TTeacherExt());
            req.setEid(t.get(0).getEid());
        }
        WeekTimetableDTO dto = tLessonTimetableService.buildWeekTimetable(req, tLessonTimetableService.TEACHER_TABLE);
        return AjaxResult.success(dto);
    }


}
